package com.pearl.utils.db;

import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class PrimeDB {

  private String primeDataDriver;
  private String primeDataUrl;
  private String primeDataUsername;
  private String primeDataPassword;

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.prime-data")
  public DataSource primeDataSource() {
    return DataSourceBuilder.create()
        .type(DruidDataSource.class)
        .driverClassName(primeDataDriver)
        .url(primeDataUrl)
        .username(primeDataUsername)
        .password(primeDataPassword)
        .build();
  }

  @Value("${spring.datasource.prime-data.driver-class-name}")
  public void setPrimeDataDriver(String primeDataDriver) {
    this.primeDataDriver = primeDataDriver;
  }

  @Value("${spring.datasource.prime-data.url}")
  public void setPrimeDataUrl(String primeDataUrl) {
    this.primeDataUrl = primeDataUrl;
  }

  @Value("${spring.datasource.prime-data.username}")
  public void setPrimeDataUsername(String primeDataUsername) {
    this.primeDataUsername = primeDataUsername;
  }

  @Value("${spring.datasource.prime-data.password}")
  public void setPrimeDataPassword(String primeDataPassword) {
    this.primeDataPassword = primeDataPassword;
  }

  public Connection create() throws Exception {
    DataSource dataSource = primeDataSource();
    return dataSource.getConnection();
  }
}
//@Component
//public class PrimeDB {
//
//  @Value("${spring.datasource.prime-data.driver-class-name}")
//  private String PrimeDataDriver;
//  @Value("${spring.datasource.prime-data.url}")
//  private String PrimeDataUrl;
//  @Value("${spring.datasource.prime-data.username}")
//  private String PrimeDataUsername;
//  @Value("${spring.datasource.prime-data.password}")
//  private String PrimeDataPassword;
//
//  @Bean
//  @ConfigurationProperties(prefix = "spring.datasource.prime-data")
//  private DataSource PrimeDataSource() {
//    return DataSourceBuilder.create()
//        .type(DruidDataSource.class)
//        .driverClassName(PrimeDataDriver)
//        .url(PrimeDataUrl)
//        .username(PrimeDataUsername)
//        .password(PrimeDataPassword)
//        .build();
//  }
//
//  public Connection create() throws Exception {
//    PrimeDB primeDB = new PrimeDB();
//    DataSource dataSource = primeDB.PrimeDataSource();
//    return dataSource.getConnection();
//  }
//
//}
